/**
 * SommeListenerImp.java
 */
package com.pluginsacceptor.plugin.formula.MathsTrigo;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataListener;
import com.pluginsacceptor.plugin.formula.MathsTrigo.support.DataSender;

/**
  */
public class SommeListenerImp extends AbstractListener {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(SommeListenerImp.class);

    /**
     * Constructor
     * @param dataListener
     * @param dataSender
     */
    public SommeListenerImp(final DataListener dataListener, final DataSender dataSender) {
        super(dataListener, dataSender);
    }

    @Override
    protected  double calculate() {
    	final MathsTrigo mtg = new MathsTrigo();

    	final List<Double> number = new ArrayList<Double>();
    	for (final String tmp : this.getUserData()) {
    	    try {
    	        number.add(Double.parseDouble(tmp));
            } catch (final Exception e) {
                LOG.warn("invalid data value '" + tmp + "'");
            }
    	}
    	final double[] primNumber = new double[number.size()];
    	for (int cnt = primNumber.length; --cnt >= 0;) {
            primNumber[cnt] = number.get(cnt);
        }
    	final double result = mtg.somme(primNumber);
		return  result;

    }
}